# -*- coding: utf-8 -*-
from Tkinter import *
import tkMessageBox as MsgBox
#from BD_InterfaceBaseDonnees import *
#from BaseDonnees import *
 
#import os
#os.environ['PATH'] ="C:/Oracle/Client/bin" + os.pathsep +os.environ['PATH'] 
#import cx_Oracle
class VueInitialisation(Frame):
    def __init__(self,master,parent,**cnf):
        KeysList=cnf.keys()
        self.parent = parent
        self.master=master
        self.width=400
        self.height=200
        if ('width' in KeysList):
            self.width=cnf['width']
        if ('width' in KeysList):
            self.height=cnf['height']
        Frame.__init__(self,master,cnf)        
        self.master.update()
        self.x=self.master.winfo_width()/2-(self.width/2)
        self.y=self.master.winfo_height()/2-(self.height/2)       
        self.place(x=self.x,y=self.y)
        self.l1=Listbox(self)
        self.l1.grid(row=0, column=0)
        self.l1.insert(END, "Emp")
        self.l1.insert(END, "Dept")
        self.l1.insert(END, "Client")
        self.l1.insert(END, "inventaire")
        self.l1.insert(END, "Produit")
        self.l1.insert(END, "Commande")
        self.l1.insert(END, "Item")
        self.l1.insert(END, "Region")
        self.l1.insert(END, "Poste")
        self.l1.insert(END, "Depot")
        self.l2=Listbox(self)
        
        self.l2.grid(row=0, column=2)
        self.center=Frame(self)
        self.B=Button(self.center, text=">", command=self.insertElement)
        self.B1=Button(self.center, text="<", command=self.deleteElement)
        self.bouton= Button(self,text="Creer", command=self.CreerTables)
        self.bouton.grid(row=1, column=2)
        self.B.pack()
        self.B1.pack()
        self.center.grid(row=0, column=1)
        #self.center.pack()
        
        #self.BaseDonnees= CommunicationBaseDonnees()
        #self.BaseDonnees.DemarrerConnection("b52equip1", "var2", "titan")
        
    def CreerTables(self):
        listeNomsTables={'Emp':tableEmp,
                         'Dept':tableDept,
                         'Client':tableClient,
                         'inventaire':tableInventaire,
                         'Produit':tableProduit,
                         'Commande':tableCommande,
                         'Item':tableItem,
                         'Region':tableRegion,                         
                         'Poste':tablePoste,
                         'Depot':tableDepot}

        listeTables=[]        
        for i in range(self.l2.size()):            
            Reponse = self.parent.BaseDonnees.CreerTable(listeNomsTables[self.l2.get(i)])
            if (Reponse[0]==0):
                MsgBox.showerror('PERP | Cr�ation des tables','Une erreur c\'est produite'+\
                                    Reponse[1])
    def insertElement(self):
        selection=self.l1.curselection()
        self.l2.insert(END, self.l1.get(selection[0]))
        self.l1.delete(selection)
        pass
    
    def deleteElement(self):
        delete=self.l2.curselection()
        self.l1.insert(END, self.l2.get(delete[0]))
        self.l2.delete(delete)
        pass
    
    def tableEmp (self):
        self.listeEmp=[]
        self.listeEmp.append(xChamp(nom="Id", type="NUMBER", taille="7"))
        self.listeEmp.append(xChamp(nom="last_name", type="VARCHAR2", taille="25"))
        self.listeEmp.append(xChamp(nom="first_name",type="VARCHAR2", taille="25"))
        self.listeEmp.append(xChamp(nom="start_date", type="Date"))
        self.listeEmp.append(xChamp(nom="manager_id", type="VARCHAR2", taille="25"))
        self.listeEmp.append(xChamp(nom="title", type="VARCHAR2", taille="25"))
        self.listeEmp.append(xChamp(nom="Dept_id", type="NUMBER", taille="7"))
        self.listeEmp.append(xChamp(nom="Salary", type="NUMBER", taille="11", precision="2"))
        
        self.matableEmp=xTable(nom="table_Emp", listechamps=self.listeEmp)
        return self.matableEmp
    
    def tableDept (self):
        self.listeDept=[]
        self.listeDept.append(xChamp(nom="Id", type="entier", taille="7"))
        self.listeDept.append(xChamp(nom="name", type="texte", taille="25"))
        self.listeDept.append(xChamp(nom="region_id", type="entier", taille="7" ))
        
        self.matableDept=xTable(nom="table_dept", listechamps=self.listeDept)
        return self.matableDept
    
    def tableClient (self):
        self.listeClient=[]
        self.listeClient.append(xChamp(nom="Id",type="entier", taille="7"))
        self.listeClient.append(xChamp(nom="last_name", type="texte", taille="25"))
        self.listeClient.append(xChamp(nom="first_name",type="texte", taille="25" ))
        self.listeClient.append(xChamp(nom="address", type="texte", taille="400"))
        self.listeClient.append(xChamp(nom="city", type="texte", taille="30"))
        self.listeClient.append(xChamp(nom="state", type="texte", taille="25"))
        self.listeClient.append(xChamp(nom="country", type="texte", taille="25"))        
        self.listeClient.append(xChamp(nom="zip_code ", type="texte", taille="20"))
        self.listeClient.append(xChamp(nom="sales_rep_id ", type="entier", taille="7"))
        self.listeClient.append(xChamp(nom="credit_rating", Type="VARCHAR2(9)", precision=""))
        self.listeClient.append(xChamp(nom="region_id", type="entier", taille="7"))
        
        self.matableClient=xTable(nom="table_Client", listechamps=self.listeClient)
        return self.matableClient
    
    
    def tableRegion (self):
        self.listeRegion=[]
        self.listeRegion.append(xChamp(nom="Id", type="entier", taille="7"))
        self.listeRegion.append(xChamp(nom="name", type="texte", taille="25"))
 
        self.matableRegion=xTable(nom="table_Region", listechamps=self.listeRegion)
        return self.matableRegion
    
    
    def tablePoste (self):
        self.listePoste=[]
        self.listePoste.append(xChamp(nom="title", type="texte", taille="25"))
 
        self.matablePoste=xTable(nom="table_dept", listechamps=self.listePste)
        return self.matablePoste
       
       
    def tableCommande (self):
       self.listeCde=[]
       self.listeCde.append(xChamp(nom="Id", type="entier", taille="7"))
       self.listeCde.append(xChamp(nom="customer_id", type="entier", taille="7"))
       self.listeCde.append((xChamp(nom="date_ordered", type="date")))
       self.listeCde.append((xChamp(nom="date_shipped", type="entier", taille="7")))
       self.listeCde.append(xChamp(nom="sales_rep_id", type="entier", taille="7"))
       self.listeCde.append(xChamp(nom="total", type="entier", taille="11", precision="2"))
       self.listeCde.append(xChamp(nom="payment_type", type="texte", taille="9"))
       self.listeCde.append(xChamp(nom="order_filled", type="texte", taille="1"))
       
       self.matableCommande=xTable(nom="table_Commande", listechamps=self.listeCommande)
       return self.matableCommande
   
    def tableDepot (self):
        self.listeDepot=[]
        self.listeDepot.append(xChamp(nom="Id", type="entier", taille="7"))
        self.listeDepot.append(xChamp(nom="address", Type="varchar2(400)", precision=""))
        self.listeDepot.append(xChamp(nom="city", type="texte", taille="30"))
        self.listeDepot.append(xChamp(nom="state", type="texte", taille="25"))
        self.listeDepot.append(xChamp(nom="country", type="texte", taille="25"))        
        self.listeDepot.append(xChamp(nom="zip_code", type="texte", taille="20"))
        self.listeDepot.append(xChamp(nom="phone", type="texte", taille="25"))
        self.listeDepot.append(xChamp(nom="manager_id ", type="entier", taille="7"))
        
        self.matableDepot=xTable(nom="table_Depot", listechamps=self.listeDepot)
        return self.matableDepot
    
    def tableInventaire(self):   
        self.listeInventaire=[]
        self.listeInventaire.append(xChamp(nom="product_id", type="entier", taille="7"))
        self.listeInventaire.append(xChamp(nom="warehouse_id", type="entier", taille="7"))
        self.listeInventaire.append(xChamp(nom="amount_in_stock", type="entier", taille="9"))
        self.listeInventaire.append(xChamp(nom="reorder_point", type="entier", taille="9"))
        self.listeInventaire.append(xChamp(nom="max_in_stock", type="entier", taille="9"))       
        
        self.matableInventaire=xTable(nom="table_Inventaire", listechamps=self.listeInventaire)
        return self.matableInventaire
    
    def tableItem(self):    
        self.listeItem=[]
        self.listeItem.append(xChamp(nom="ord_id", type="entier", taille="7"))
        self.listeItem.append(xChamp(nom="item_id ", type="entier", taille="7"))
        self.listeItem.append(xChamp(nom="product_id", type="entier", taille="7"))        
        self.listeItem.append(xChamp(nom="price", type="entier", taille="11", precision="2"))
        self.listeItem.append(xChamp(nom="quantity", type="entier", taille="9"))
        self.listeItem.append(xChamp(nom="quantity_shipped", type="entier", taille="9"))
        
        self.matableItem=xTable(nom="table_Item", listechamps=self.listeItem)
        return self.matableItem
    
    
    def tableProduit(self):    
        self.listeProduit=[]
        self.listeProduit.append(xChamp(nom="product_id", type="entier", taille="7"))
        self.listeProduit.append(xChamp(nom="name", type="texte", taille="50"))       
        self.listeProduit.append(xChamp(nom="price", type="entier", taille="11", precision="2"))
        self.listeProduit.append(xChamp(nom="quantity", type="entier", taille="9"))
      
        self.matableProduit=xTable(nom="table_Produit", listechamps=self.listeProduit)
        return self.matableProduit
        
       
       